//
//  BookWeek.swift
//  GOFM
//
//  Created by siri on 2024/10/22.
//  Copyright © 2024 Glad. All rights reserved.
//

import SwiftUI



struct IdleTimeWeek: View {
    let weekArray = ["日","一","二","三","四","五","六"]
    // 滚动索引
    @Binding  var scrollToIndex: Int?
    var body: some View {
        HStack(spacing: 0){
                            Rectangle().frame(width: 50,height: 40).foregroundColor(.white)
                                .overlay(VStack(spacing: 0) {
                                    Text("六").font(.system(size: 17)).foregroundColor(Color(hex: "7f8182"))
                                    Text("20").font(.system(size: 17)).foregroundColor(Color(hex: "7f8182"))
                                })
                            Spacer()
                            ScrollViewReader { proxy in
                                ScrollView(.horizontal,showsIndicators: false) {
                                    HStack {
                                        ForEach(0...2,id:\.self) {
                                            index in
                                            HStack(spacing: 0) {
      
                                                ForEach(0...6, id: \.self) { index in
                                                    VStack (spacing: 0){
                                                        Text("\(weekArray[index])")
                                                            .foregroundColor(Color(hex:"#383b3d"))
                                                            .frame(width: 45,height: 20).font(.system(size: 17))
                                                        Text("20")
                                                            .foregroundColor(Color(hex: "#383b3d"))
                                                            .frame(width: 45,height: 20).font(.system(size: 17))
                                                    }
                                                }
      
                                            }.id(0).gesture(
                                              DragGesture()
                                                  .onChanged({ value in
                                                      if  value.translation.width < 0 {
                                                          scrollToIndex = 1
                                                      }else {
                                                          scrollToIndex = 0
                                                      }
                                                  })
                                            )
                                        }
                                    }
                                }.onChange(of: scrollToIndex) { newValue in
                                    if let newValue {
                                        withAnimation {
                                            print(newValue)
                                            proxy.scrollTo(newValue, anchor: .leading)
                                        }
                                    }
                                }
                            }.frame(width: 315,height: 40).background(Color.g_color_bg_white)
            }
    }
}
//
//struct BookWeek_Previews: PreviewProvider {
//    static var previews: some View {
//        BookWeek()
//    }
//}
